package com.meichis.yndms;

import android.app.Activity;
import android.content.Context;
import android.content.IntentFilter;
import android.text.TextUtils;

import androidx.annotation.NonNull;
import androidx.lifecycle.ViewModelProvider;
import androidx.lifecycle.ViewModelStore;
import androidx.lifecycle.ViewModelStoreOwner;
import androidx.multidex.MultiDex;

import com.meichis.mcsappframework.BaseApplication;
import com.meichis.mcsappframework.utils.SharePreferenceUtil;
import com.meichis.mcsappframework.utils.Tools;
import com.meichis.yndms.receiver.HeartBeatReceiver;
import com.meichis.yndms.receiver.UpLoadReceiver;
import com.tencent.bugly.crashreport.CrashReport;

import java.util.UUID;


public class MCApplication extends BaseApplication implements ViewModelStoreOwner {

    private ViewModelStore viewModelStore;
    private ViewModelProvider.Factory mFactory;
    public static String DownLoadUrl = "";
    public static String DeviceID = "";
    private HeartBeatReceiver heartBeatReceiver;
    private UpLoadReceiver upLoadReceiver;

    @Override
    public void onCreate() {
        super.onCreate();
        viewModelStore = new ViewModelStore();
        DownLoadUrl = SharePreferenceUtil.getInstance().getStringValue(SharePreferenceUtil.PREFERENCES_DOWNLOADURL) != null ?
                SharePreferenceUtil.getInstance().getStringValue(SharePreferenceUtil.PREFERENCES_DOWNLOADURL) : "";
        if (TextUtils.isEmpty(DownLoadUrl)) {
            DownLoadUrl = "-,-.-!-";
        }
        //心跳广播
        heartBeatReceiver = new HeartBeatReceiver();
        registerReceiver(heartBeatReceiver, new IntentFilter("com.meichis.yndms.intent.HEARTBEAT"));
        //上传广播
        upLoadReceiver = new UpLoadReceiver();
        registerReceiver(upLoadReceiver, new IntentFilter("com.meichis.yndms.intent.UpLoadInfo"));

        initCrash();
        getDeviceID();
    }


    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }

    @Override
    @NonNull
    public ViewModelStore getViewModelStore() {
        return viewModelStore;
    }

    /**
     * 暴露出去 对外提供唯一一份的ViewModelProvider
     *
     * @param activity
     * @return
     */
    public ViewModelProvider getAppViewModelProvider(Activity activity) {
        return new ViewModelProvider((MCApplication) activity.getApplicationContext(),
                ((MCApplication) activity.getApplicationContext()).getAppFactory(activity));
    }

    private ViewModelProvider.Factory getAppFactory(Activity activity) {
        if (mFactory == null) {
            mFactory = ViewModelProvider.AndroidViewModelFactory.getInstance(activity.getApplication());
        }
        return mFactory;
    }

    private void initCrash() {
        // 获取当前包名
        String packageName = getPackageName();
        // 获取当前进程名
        String processName = Tools.getProcessName(android.os.Process.myPid());
        // 设置是否为上报进程
        CrashReport.UserStrategy strategy = new CrashReport.UserStrategy(this);
        strategy.setUploadProcess(processName == null || processName.equals(packageName));
        // 初始化Bugly
        CrashReport.initCrashReport(this, "474d481531", BuildConfig.DEBUG, strategy);
    }

    private void getDeviceID() {
        DeviceID = Tools.getDeviceId(this);
        if (TextUtils.isEmpty(DeviceID) || DeviceID.equals("unknown") || DeviceID.equals("ANDROID_ID_IS_NULL")) {
            DeviceID = SharePreferenceUtil.getInstance().getStringValue("DeviceID");
            if (TextUtils.isEmpty(DeviceID)) {
                DeviceID = UUID.randomUUID().toString();
                SharePreferenceUtil.getInstance().setStringValue("DeviceID", DeviceID);
            }
        }
    }

    @Override
    public void onTerminate() {
        unregisterReceiver(heartBeatReceiver);
        super.onTerminate();
    }
}